package com.banshou.installs.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.InstallHisBinding;
import com.banshou.installs.model.dao.ResponseInstallHis;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.InstallHisAdapter;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.ToastUtil;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cjq on 2018/1/3.
 * Email: stoic_yb@139.com
 * features:
 */

public class InstallHisActivity extends BaseActivity {
    private InstallHisBinding mBinding;
    private List<ResponseInstallHis.map> mapList;
    private InstallHisAdapter mAdapter;
    private DialogUtils dialogUtils;
    private String from2Floorset;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        from2Floorset = getIntent().getStringExtra(Config.FLOORSET_HISTORY);
        mBinding = DataBindingUtil.setContentView(this, R.layout.install_his);
        initView();
    }

    private void initView() {
        mapList = new ArrayList<>();
        dialogUtils = new DialogUtils();
        mAdapter = new InstallHisAdapter(this, R.layout.install_his_item, mapList);
        mBinding.recycleview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mBinding.recycleview.setAdapter(mAdapter);
        mBinding.title.pagerTitle.setText(R.string.self_hisin);
        mBinding.title.back.setOnClickListener(view -> {
            if (from2Floorset != null) {
                Intent intent = new Intent(InstallHisActivity.this, MainActivity.class);
                startActivity(intent);
            }
            finish();
        });
        getList();
        mAdapter.setListener((id, position) -> {
            if (mapList.size() >= position) {
                ResponseInstallHis.map map = mapList.get(position);
                if (map.getStatus().getId().equals("1")) {
                    Intent intent = new Intent(InstallHisActivity.this, FloorSetActivity.class);
                    intent.putExtra(Config.STARTLC, map.getStartFloor());
                    intent.putExtra(Config.ENDLC, map.getEndFloor());
                    intent.putExtra(Config.ELEID, id);
                    startActivity(intent);
                } else if(map.getStatus().getId().equals("2")){
                    Intent intent = new Intent(InstallHisActivity.this, EleInfoActivity.class);
                    intent.putExtra(Config.CLICKELEID, id);
                    startActivity(intent);
                }else{
                    ToastUtils.showShort("该电梯已禁用");
                }
            }
        });
    }

    private void getList() {
        dialogUtils.showAni(InstallHisActivity.this);
        RetrofitManager2.getInstance()
                .create(ResponseInfo.class)
                .installHis()
                .compose(RxUtil.lifecycle(this))
                .subscribe(new Observer<ResponseInstallHis>() {
                    @Override
                    protected void onNextStep(ResponseInstallHis responseInstallHis) {
                        dialogUtils.hideAni();
                        if (responseInstallHis.getList() != null) {
                            mapList.clear();
                            mapList.addAll(responseInstallHis.getList());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast("没有安装信息");
                        }
                    }

                    @Override
                    protected void onErrorResult() {
                        dialogUtils.hideAni();
                    }
                });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (from2Floorset != null) {
            Intent intent = new Intent(InstallHisActivity.this, MainActivity.class);
            startActivity(intent);
        }
        finish();
    }

}
